import request from '@/utils/request'

// ==================== 文档管理 ====================

// 分页查询文档
export function getDocumentPage(params) {
  return request({
    url: '/document/pageDocument',
    method: 'get',
    params
  })
}

// 获取文档详情
export function getDocumentDetail(id) {
  return request({
    url: `/document/getDocumentById/${id}`,
    method: 'get'
  })
}

// 创建文档
export function createDocument(data) {
  return request({
    url: '/document/addOrUpdateDocument',
    method: 'post',
    data
  })
}

// 更新文档
export function updateDocument(data) {
  return request({
    url: '/document/addOrUpdateDocument',
    method: 'post',
    data
  })
}

// 删除文档
export function deleteDocument(id) {
  return request({
    url: `/document/deleteDocument/${id}`,
    method: 'delete'
  })
}

// 批量删除文档
export function deleteBatchDocument(ids) {
  return request({
    url: '/document/deleteBatch',
    method: 'post',
    data: ids
  })
}

// 发布文档
export function publishDocument(id) {
  return request({
    url: `/document/publish/${id}`,
    method: 'post'
  })
}

// 下架文档
export function unpublishDocument(id) {
  return request({
    url: `/document/unpublish/${id}`,
    method: 'post'
  })
}

// 分享文档
export function shareDocument(id, shareType, expireTime) {
  return request({
    url: `/document/share/${id}`,
    method: 'post',
    params: { shareType, expireTime }
  })
}

// 取消分享
export function unshareDocument(id) {
  return request({
    url: `/document/unshare/${id}`,
    method: 'post'
  })
}

// 搜索文档
export function searchDocuments(keyword, limit = 20) {
  return request({
        url: '/document/search',
    method: 'get',
    params: { keyword, limit }
  })
}

// 获取我的文档
export function getMyDocuments(params) {
  return request({
    url: '/document/my',
    method: 'get',
    params
  })
}

// 获取共享文档
export function getSharedDocuments(params) {
  return request({
    url: '/document/shared',
    method: 'get',
    params
  })
}

// 获取最近文档
export function getRecentDocuments(limit = 10) {
  return request({
    url: '/document/recent',
    method: 'get',
    params: { limit }
  })
}

// ==================== 文档版本管理 ====================

// 获取版本历史
export function getDocumentVersions(documentId, params) {
  return request({
    url: `/document-version/versions/${documentId}`,
    method: 'get',
    params
  })
}

// 获取版本详情
export function getDocumentVersionDetail(versionId) {
  return request({
    url: `/document-version/version/${versionId}`,
    method: 'get'
  })
}

// 创建版本
export function createDocumentVersion(data) {
  return request({
    url: '/document-version/createVersion',
    method: 'post',
    data
  })
}

// 恢复版本
export function restoreDocumentVersion(documentId, versionId) {
  return request({
    url: `/document-version/restore/${documentId}/${versionId}`,
    method: 'post'
  })
}

// 比较版本
export function compareDocumentVersions(documentId, version1, version2) {
  return request({
    url: `/document-version/compare/${documentId}`,
    method: 'get',
    params: { version1, version2 }
  })
}

// 删除版本
export function deleteDocumentVersion(versionId) {
  return request({
    url: `/document-version/deleteVersion/${versionId}`,
    method: 'delete'
  })
}

// ==================== 文档分类管理 ====================

// 获取分类列表
export function getDocumentCategoryList() {
  return request({
    url: '/document-category/categoryList',
    method: 'get'
  })
}

// 获取分类树
export function getDocumentCategoryTree() {
  return request({
    url: '/document-category/tree',
    method: 'get'
  })
}

// 获取分类详情
export function getDocumentCategoryDetail(id) {
  return request({
    url: `/document-category/categoryDetail/${id}`,
    method: 'get'
  })
}

// 创建分类
export function createDocumentCategory(data) {
  return request({
    url: '/document-category/addOrUpdateCategory',
    method: 'post',
    data
  })
}

// 更新分类
export function updateDocumentCategory(data) {
  return request({
    url: '/document-category/addOrUpdateCategory',
    method: 'post',
    data
  })
}

// 删除分类
export function deleteDocumentCategory(id) {
  return request({
    url: `/document-category/deleteCategory/${id}`,
    method: 'delete'
  })
}

// ==================== 文档权限管理 ====================

// 获取文档权限
export function getDocumentPermissions(documentId) {
  return request({
    url: `/document-permission/permissions/${documentId}`,
    method: 'get'
  })
}

// 设置文档权限
export function setDocumentPermissions(documentId, data) {
  return request({
    url: `/document-permission/setPermissions/${documentId}`,
    method: 'post',
    data
  })
}

// 添加协作者
export function addDocumentCollaborator(documentId, data) {
  return request({
    url: `/document-permission/addCollaborator/${documentId}`,
    method: 'post',
    data
  })
}

// 移除协作者
export function removeDocumentCollaborator(documentId, userId) {
  return request({
    url: `/document-permission/removeCollaborator/${documentId}/${userId}`,
    method: 'delete'
  })
}

// 更新协作者权限
export function updateCollaboratorPermissions(documentId, userId, permissions) {
  return request({
    url: `/document-permission/updatePermissions/${documentId}/${userId}`,
    method: 'put',
    data: permissions
  })
}

// ==================== 文档模板管理 ====================

// 分页查询文档模板
export function getTemplatePage(params) {
  return request({
    url: '/document-template/pageTemplate',
    method: 'get',
    params
  })
}

// 获取模板详情
export function getTemplateDetail(id) {
  return request({
    url: `/document-template/getTemplateById/${id}`,   
    method: 'get'
  })
}

// 创建模板
export function createTemplate(data) {
  return request({
    url: '/document-template/addOrUpdateTemplate',
    method: 'post',
    data
  })
}

// 更新模板
export function updateTemplate(id, data) {
  return request({
    url: '/document-template/addOrUpdateTemplate',
    method: 'post',
    data: { ...data, id }
  })
}

// 删除模板
export function deleteTemplate(id) {
  return request({
    url: `/document-template/deleteTemplate/${id}`,
    method: 'delete'
  })
}

// 更新模板状态
export function updateTemplateStatus(data) {
  return request({
    url: '/document-template/updateStatus',
    method: 'post',
    data
  })
}

// 复制模板
export function copyTemplate(id) {
  return request({
    url: `/document-template/copy/${id}`,
    method: 'post'
  })
}

// 导出模板
export function exportTemplates(params) {
  return request({
    url: '/document-template/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 从模板创建文档
export function createDocumentFromTemplate(templateId, data) {
  return request({
    url: `/document-template/createFromTemplate/${templateId}`,
    method: 'post',
    data
  })
}
